$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='pmsPatientId']").val("");
	$("[name='pmsPatientFirstname']").val("");
	$("[name='pmsPatientLastname']").val("");
});

function getPatientSearchResult() {
	$("#pmsPatientSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#pmsDoc").empty();
	$("#pmsStf").empty();
	$.getJSON("doctor/searchResultDefault.html?searchUserId="+$("[name='pmsPatientId']").val()+"&searchFirstname="+$("[name='pmsPatientFirstname']").val()+"&searchLastname="+$("[name='pmsPatientLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.patientSearchResult)){
			$("#pmsPatientSearchResult").data("patientSearchResult", data);
			printTableBody(data);
			$("#pmsPatientSearchResult").append("<br /><button id=\"confirmPa\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>");
		}else{
			$("#pmsPatientSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function clearPtable(){
	$("#pmsPatientSearchResult").empty();
}

function printTableBody(data) {
	
	$("#pmsPatientSearchResult")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>Patient id</th>" +
					"<th>First name</th>" +
					"<th>Last name</th>" +
					"<th>Gender</th>" +
					"<th>Date of Birth</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.patientSearchResult).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"patientRadio\" value=\""+v.userId+"\"></td>");
					tr.append("<td>" + v.userId + "</td>");
					tr.append("<td>" + v.firstname + "</td>");
					tr.append("<td>" + v.lastname + "</td>");
					var gen=v.gender;
					if(gen=='m'){
						tr.append("<td>male</td>");
					}else if(gen=='f'){
						tr.append("<td>female</td>");
					}else{
						tr.append("<td></td>");
					}
					tr.append("<td>" + v.dateOfBirth.substring(0,10) +"</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0 : {
				sorter : false
			}
		}
	});
}

function confirmPatient(){
	if($("[name=\"patientRadio\"]:checked").length<=0){
		return;
	}
	var id=$("[name=\"patientRadio\"]:checked").val();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#pmsDoc").data("current",id);
	$.getJSON("doctor/pmsDocStf.html?lookupPmsId="+id, function(data) {
		if(!jQuery.isEmptyObject(data.grantedDoc)){
			$("#pmsDoc").empty();
//			$("#pmsDoc").data("docGranted", data.grantedDoc);
			printDocTableBody(data.grantedDoc);
		}else{
			$("#pmsDoc").empty();
			$("#pmsDoc").append("<strong>No granted doctor.</strong>");
		}
		if(!jQuery.isEmptyObject(data.grantedStf)){
			$("#pmsStf").empty();
//			$("#pmsStf").data("stfGranted", data.grantedStf);
			printStfTableBody(data.grantedStf);
		}else{
			$("#pmsStf").empty();
			$("#pmsStf").append("<strong>No granted staff.</strong>");
		}
		
	});
}

function printDocTableBody(grantedDoc){
	$("#pmsDoc")
	.append(
			"<table id=\"doclist\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th>Doctor id</th>" +
			"<th>First name</th>" +
			"<th>Last name</th>" +
			"<th>Gender</th>" +
			"<th>Office phone number</th>" +
			"<th>Office room</th>" +
			"<th>Department</th>" +
			"<th></th>"+
			"</tr>" +
			"</thead>" +
			"<tbody id=\"docTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;

		$(grantedDoc).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td>" + v.userId + "</td>");
			tr.append("<td>" + v.firstname + "</td>");
			tr.append("<td>" + v.lastname + "</td>");
			var gen=v.gender;
			if(gen=='m'){
				tr.append("<td>male</td>");
			}else if(gen=='f'){
				tr.append("<td>female</td>");
			}else{
				tr.append("<td></td>");
			}
			tr.append("<td>" +v.officeInfo.officePhone+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.officeRoom+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.deptInfo.deptName+"</td>");
			tr.append("<td><button class=\"button\" onclick=\"removeDocPms("+v.userId+")\">remove</button></td>");
			tr.append("</tr>");
			$("#docTableBody").append(tr);
		});
$("#doclist").tablesorter({
	widgets : [ 'zebra' ],
	headers : {
		7 : {
			sorter : false
		}
	}
});
}

function printStfTableBody(grantedStf){
	$("#pmsStf")
	.append(
			"<table id=\"stflist\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th>Staff id</th>" +
			"<th>First name</th>" +
			"<th>Last name</th>" +
			"<th>Gender</th>" +
			"<th>Office phone number</th>" +
			"<th>Office room</th>" +
			"<th>Department</th>" +
			"<th></th>"+
			"</tr>" +
			"</thead>" +
			"<tbody id=\"stfTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;

		$(grantedStf).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td>" + v.userId + "</td>");
			tr.append("<td>" + v.firstname + "</td>");
			tr.append("<td>" + v.lastname + "</td>");
			var gen=v.gender;
			if(gen=='m'){
				tr.append("<td>male</td>");
			}else if(gen=='f'){
				tr.append("<td>female</td>");
			}else{
				tr.append("<td></td>");
			}
			tr.append("<td>" +v.officeInfo.officePhone+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.officeRoom+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.deptInfo.deptName+"</td>");
			tr.append("<td><button class=\"button\" onclick=\"removeStfPms("+v.userId+")\">remove</button></td>");
			tr.append("</tr>");
			$("#stfTableBody").append(tr);
		});
$("#stflist").tablesorter({
	widgets : [ 'zebra' ],
	headers : {
		7 : {
			sorter : false
		}
	}
});
}

function removeDocPms(docId){
	var patientId=$("#pmsDoc").data("current");
	$.getJSON("doctor/removeDocPermission.html?removePmsId="+patientId+"&removePmsDocId="+docId,function(data){
		if(data.flag==true){
			if(!jQuery.isEmptyObject(data.grantedDoc)){
				$("#pmsDoc").empty();
//				$("#pmsDoc").data("docGranted", data.grantedDoc);
				printDocTableBody(data.grantedDoc);
			}else{
				$("#pmsDoc").empty();
				$("#pmsDoc").append("<strong>No granted doctor.</strong>");
			}
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
		
	});
}

function removeStfPms(staffId){
	var patientId=$("#pmsDoc").data("current");
	$.getJSON("doctor/removeStfPermission.html?removePmsId="+patientId+"&removePmsStfId="+staffId,function(data){
		if(data.flag==true){
			if(!jQuery.isEmptyObject(data.grantedStf)){
				$("#pmsStf").empty();
//				$("#pmsStf").data("stfGranted", data.grantedStf);
				printStfTableBody(data.grantedStf);
			}else{
				$("#pmsStf").empty();
				$("#pmsStf").append("<strong>No granted staff.</strong>");
			}
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
		
	});
}
